package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.utils.resources.MessageConstant;
import io.swagger.annotations.Authorization;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestController
@RequestMapping("report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private SetMealService setMealService;

    @GetMapping("getMemberCount")
    public Result getMemberCount(){
        try {
            Map map = memberService.getMemberCount();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @GetMapping("getSetmealProportion")
    public Result getSetmealProportion(){
        try {
            List<Map> mapList = setMealService.getSetmealProportion();
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @GetMapping("getReportData")
    public Result getReportData(){
        Map map = memberService.getDataCount();
        return new Result(map);
    }

    @RequestMapping("exportExcel")
    public String exportExcel(HttpServletResponse response){
        Map map = (Map) this.getReportData().getData();

        //在内存中创建一个Excel文件
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();

        XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
        //居中
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);

        //创建工作表，指定工作表名称
        XSSFSheet sheet = xssfWorkbook.createSheet("统计分析");
        //创建行，0表示第一行
        XSSFRow row = sheet.createRow(0);
        //创建单元格，0表示第一个单元格
        row.setRowStyle(cellStyle);
        row.createCell(0).setCellValue("会员数据统计");
        sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));

        XSSFRow row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("今日新增会员数");
        row1.createCell(1).setCellValue((Integer)map.get("todayNewMember"));
        row1.createCell(2).setCellValue("总会员数");
        row1.createCell(3).setCellValue((Integer)map.get("totalMember"));

        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("本周新增会员数");
        row2.createCell(1).setCellValue((Integer)map.get("thisWeekNewMember"));
        row2.createCell(2).setCellValue("本月新增会员数");
        row2.createCell(3).setCellValue((Integer)map.get("thisMonthNewMember"));

        XSSFRow row3 = sheet.createRow(3);
        row3.createCell(0).setCellValue("预约到诊数据统计");
        sheet.addMergedRegion(new CellRangeAddress(3,3,0,3));

        XSSFRow row4 = sheet.createRow(4);
        row4.createCell(0).setCellValue("今日预约数");
        row4.createCell(1).setCellValue((Integer)map.get("todayOrderNumber"));
        row4.createCell(2).setCellValue("今日到诊数");
        row4.createCell(3).setCellValue((Integer)map.get("todayVisitsNumber"));

        XSSFRow row5 = sheet.createRow(5);
        row5.createCell(0).setCellValue("本周预约数");
        row5.createCell(1).setCellValue((Integer)map.get("thisWeekOrderNumber"));
        row5.createCell(2).setCellValue("本周到诊数");
        row5.createCell(3).setCellValue((Integer)map.get("thisWeekVisitsNumber"));

        XSSFRow row6 = sheet.createRow(6);
        row6.createCell(0).setCellValue("本月预约数");
        row6.createCell(1).setCellValue((Integer)map.get("thisMonthOrderNumber"));
        row6.createCell(2).setCellValue("本月到诊数");
        row6.createCell(3).setCellValue((Integer)map.get("thisMonthVisitsNumber"));

        XSSFRow row7 = sheet.createRow(7);
        row7.createCell(0).setCellValue("热门套餐");
        sheet.addMergedRegion(new CellRangeAddress(7,7,0,3));

        XSSFRow row8 = sheet.createRow(8);
        row8.createCell(0).setCellValue("套餐名称");
        row8.createCell(1).setCellValue("预约数量");
        row8.createCell(2).setCellValue("占比");
        row8.createCell(3).setCellValue("备注");

        List<Map> mapList = (List<Map>) map.get("hotSetmeal");
        for (int i = 0; i < mapList.size(); i++) {
            XSSFRow rows = sheet.createRow(9+i);
            rows.createCell(0).setCellValue(String.valueOf(mapList.get(i).get("name")));
            rows.createCell(1).setCellValue(String.valueOf(mapList.get(i).get("count")));
            rows.createCell(2).setCellValue(String.valueOf(mapList.get(i).get("rate")));
            rows.createCell(3).setCellValue((String) mapList.get(i).get("remark"));
        }

        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("report.xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            xssfWorkbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            xssfWorkbook.close();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "导出信息出错";
        }
    }
}
